package org.devops

def GetCode(){
    checkout scmGit(branches: [[name: "${env.branchName}"]], 
                            extensions: [], 
                            userRemoteConfigs: [[credentialsId: '0b6bd2f0-58d1-4782-bdde-15a9ca30619b', 
                            url: "${env.srcUrl}"]])
    sh "ls -l "
}

def Build(jdks){
    //下载settings.xml
    settings = libraryResource 'settings.xml'
    writeFile file: 'settings.xml', text: settings
    if ("${env.serviceName}" == "pig-upms"){
        sh """
        export JAVA_HOME=${jdks["${params.JdkVersion}"]}
        echo \$JAVA_HOME
        export PATH=\$JAVA_HOME/bin:\$PATH
        java -version
        ls -l 
        mvn clean package -s settings.xml
        #mvn clean package -pl pig-upms-biz -am -f ${env.serviceName}/pom.xml -s settings.xml  
        ls -l ${env.serviceName}/pig-upms-biz/target/
    """
    } else{
        sh """
                export JAVA_HOME=${jdks["${params.JdkVersion}"]}
                echo \$JAVA_HOME
                export PATH=\$JAVA_HOME/bin:\$PATH
                java -version
                ls -l 
                mvn clean package -pl  ${env.serviceName} -am  -s settings.xml
                ls -l ${env.serviceName}/target/
            """
    }
}

def PushArtifacts(){
    sh """
        mvn deploy:deploy-file \
        -s settings.xml \
        -DgroupId=com.pig \
        -DartifactId=${env.serviceName} \
        -Dversion=${env.branchName} \
        -Dpackaging=jar \
        -Dfile=${env.serviceName}/target/${env.serviceName}.jar \
        -Durl=http://nexus.idevops.site/repository/pig/  \
        -DrepositoryId=pig
    """
}


def BuildDockerImages(){
    repoName = "pigservice"
    // Get config.json
    data = libraryResource 'config.json'
    //println(data)
    writeFile file: '/kaniko/.docker/config.json', text: data

    //Build Docker images
    env.dockerImageName = "ccr.ccs.tencentyun.com/${repoName}/${env.serviceName}:${env.branchName}"
    sh """
        export DOCKER_CONFIG="/kaniko/.docker/" 
        ls -l /kaniko/.docker/config.json
        /kaniko/executor  \
        --dockerfile=./${env.serviceName}/Dockerfile \
        --context=./${env.serviceName} \
        --destination=${env.dockerImageName}
    """
}